<template>
  <el-dialog
    :visible.sync="dialogOpen"
    :title="dialogTitle"
    :before-close="cancel"
    width="30%"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="机场名称" prop="airPortName">
            <el-input v-model="formData.airPortName" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="经度" prop="longitude">
            <el-input v-model="formData.longitude" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="纬度" prop="latitude">
            <el-input v-model="formData.latitude" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="目标类型" prop="targetType">
            <el-input v-model="formData.targetType" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="目标数量" prop="targetCount">
            <el-input v-model="formData.targetCount" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="目标区域" prop="targetArea">
            <el-input v-model="formData.targetArea" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="国家" prop="country">
            <el-input v-model="formData.country" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="formData.remark"
              type="textarea"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<style scoped>
.dialog-footer {
  text-align: center;
}
</style>
<script>
import {
  addAirPlane,
  updateAirPlane,
} from "@/api/service";

export default {
  name: "Edit",
  props: [
    "dialogTitle", //标题
    "dialogOpen", //显示
    "dialogData",
  ],
  data() {
    return {
      sourceCodeDatas: [], //数据源编码
      formData: {},
      isMysql: false,
      // 表单校验
      rules: {
        airPortName: [
          { required: true, message: "机场名称不能为空", trigger: "blur" },
        ],
        longitude: [
          { required: true, message: "经度不能为空", trigger: "blur" },
        ],
        latitude: [
          { required: true, message: "纬度不能为空", trigger: "blur" },
        ],
        targetArea: [
          { required: true, message: "目标区域不能为空", trigger: "blur" },
        ],
        targetCount: [
          { required: true, message: "目标数量不能为空", trigger: "blur" },
        ],
        targetType: [
          { required: true, message: "目标类型不能为空", trigger: "blur" },
        ],
        country: [
          { required: true, message: "国家不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {},
  created() {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.formData = this.dialogData;
      console.log(119, "formdata:", this.formData);
    },
    // 取消按钮
    cancel() {
      this.$emit("closeDialog");
    },
    // 表单重置
    reset() {
      this.formData = {
        sourceName: undefined,
        sourceCode: undefined,
        sourceType: undefined,
        sourceDesc: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        console.log("submitForm", this.formData)
        if (valid) {
          if (this.formData.id != undefined) {
            console.log('update');
            updateAirPlane(this.formData).then((response) => {
              console.log(response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          } else {
            addAirPlane(this.formData).then((response) => {
              console.log('add', response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          }
        }
      });
    },
    // 测试数据源
    testPreview() {
      const dataSetParamDtoList = [];
      const dataSetTransformDtoList = [];
      console.log(209, this.formData);
      const data = {
        dataSetParamDtoList: dataSetParamDtoList,
        dataSetTransformDtoList: dataSetTransformDtoList,
        dynSentence: this.formData.dynSentence,
        setType: "mysql",
        sourceCode: this.formData.sourceCode,
      };
      console.log(196, data);
      testTransform(data).then((res) => {
        console.log("testTransform", res);
        if (response.code == 200) {
          this.$message({ message: "操作成功", type: "success" });
        } else {
          this.$message({ message: response.message, type: "error" });
        }
      });
    },
  },
};
</script>
